package com.fenshi.until;

import android.content.res.Resources;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SystemUtils {
    public static int getScreenOrientation() {
        return Resources.getSystem().getConfiguration().orientation;
    }

    public static boolean isPhone(String phone) {
        String reg = "^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
        Pattern p = Pattern
                .compile(reg);
        Matcher m = p.matcher(phone);
        System.out.println(m.matches() + "---");
        return m.matches();
    }

    //Bean生成Map
    public static Map<String, Object> beanToMap(Object bean) {
        Map<String, Object> result = new HashMap<String, Object>();
        Method[] methods = bean.getClass().getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().startsWith("get")) {
                String field = method.getName();
                field = field.toLowerCase().substring(3);
                Object value = null;
                try {
                    value = method.invoke(bean, null);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                result.put(field, value == null ? "" : value.toString());
            }
        }
        return result;
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        String date = sDateFormat.format(new java.util.Date());
        return date;
    }


}
